﻿(function () {
   
    var Game = {};

    var GAME_WIDTH = 8;
    var GAME_HEIGHT = 6;

    Game.factory = new GemFactory();
    Game.renderer = new Renderer();
    Game.field = Game.generateField();

    Game.onGemClick = function (e) {
        if (dragged === null) {
            var gem = e.target;
            dragged = gem;

            var gemRow = gem.id.charAt(0);
            var gemCol = gem.id.charAt(2);
            var gemType = gem.classList[1].substring(4, gem.classList[1].length);
            gemType = gemType[0].toUpperCase() + gemType.substring(1, gemType.length);
            //field[gemRow][gemCol + 1] = GemColor[gemType];
            dragged = null;
        }
    }   

    Game.generateField = function () {
        var gameField = new Field(GAME_WIDTH, GAME_HEIGHT);

        var matrix = [];

        for (var row = 0; row < gameField.Height; row++) {

            var rowElements = [];
           
            for (var col = 0; col < gameField.Width; col++) {
                rowElements[col] = this.factory.produceGem(row, col);
            }

            matrix.push(rowElements);
        }

        gameField.matrix = matrix;
        return gameField;
    }

    

    Game.init = function () {
        var wrapper = document.getElementById("wrapper");
        wrapper.addEventListener("click", Game.onGemClick);

        this.renderer = new Renderer(this.field, wrapper);
        this.renderer.render();
    }


    //var field =
    //[
    //    [GemColor.Blue, GemColor.Red, GemColor.Green, GemColor.Purple, GemColor.Blue],
    //    [GemColor.Purple, GemColor.Green, GemColor.Yellow, GemColor.Red, GemColor.Green],
    //    [GemColor.Blue, GemColor.Red, GemColor.Green, GemColor.Purple, GemColor.Blue],
    //];

    

    var dragged = null;
    var render = false;


    //renderField();
    //setInterval(renderField, 800);
    Game.init();
   
})();

